<?php
/**
 * Created by PhpStorm.
 * Author: zhugege
 * Date: 2018/3/7
 * Time: 16:30
 */
namespace App\Http\Admin\Personnel\Controllers;

use Framework\BaseClass\Http\Admin\Controller;
use Illuminate\Http\Request;
use App\Http\Admin\Personnel\Models\TrainQuestion;

class TrainQuestionController extends Controller
{

    /**
     * 问题列表
     * @author zhugege
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function trainQuestionList()
    {
        try {
            if (request()->isMethod('post')) {
                $params = $this->getRequestParameters(['pageCurrent', 'pageSize']);
                $trainQuestion = new TrainQuestion();
                $trainQuestionList = $trainQuestion->getTrainQuestionList($params['pageCurrent'], $params['pageSize']);

                return $this->returnPagingData($trainQuestionList[0],(int)$params['pageCurrent'],$trainQuestionList[1]);
            }
        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }
        return $this->view('train-question-list');
    }

    /**
     * 添加问题
     * @author zhugege
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function trainQuestionAdd()
    {
        try {
            if (request()->isMethod('post')) {
                debug(request()->all());
                $params = request()->all();
                $trainQuestion = new TrainQuestion();

                //插入答案
                $saveId = $trainQuestion->addTrainQuestion($params['question'], $params['type'], $params['score']);
                $result = $trainQuestion->addTrainAnswer($saveId, $params['type'], $params['answer'], $params['correct-answer'], $params['decide-answer']);

                return $result ? $this->ajaxSuccess('添加成功', [
                    'dataGridId' => 'train-question-list'
                ]) : $this->ajaxFail();
            }
        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }
        return $this->view('train-question-add');
    }

    /**
     * 删除问题
     * @author zhugege
     * @return array
     */
    public function trainQuestionDel()
    {
        $params = $this->getRequestParameters(['id']);
        $trainQuestion = new TrainQuestion();
        //删除question表
        $trainQuestion->delTrainQuestion($params['id']);
        //删除answer表
        $trainQuestion->delTrainAnswer($params['id']);

        return $this->ajaxSuccess('删除成功');
    }

    /**
     * 编辑问题
     * @author zhugege
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function trainQuestionEdit()
    {
        try {
            if (request()->isMethod('post')) {
                debug(request()->all());
                $params = request()->all();
                $trainQuestion = new TrainQuestion();
                $trainQuestion->editTrainQuestion($params['id'], $params['question'], $params['score']);
                $result=true;
                if($params['type']!=4){
                    //删除原先的答案
                    $trainQuestion->delTrainAnswer($params['id']);
                    //插入新的答案
                    $result = $trainQuestion->addTrainAnswer($params['id'], $params['type'], $params['answer'], $params['correct-answer'], $params['decide-answer']);
                }
                return [

                ];
//                return $result ? $this->ajaxSuccess('添加成功', [
//                    'dataGridId' => 'train-question-list'
//                ]) : $this->ajaxFail();
            }
            $params = $this->getRequestParameters(['id']);
            $trainQuestion = new TrainQuestion();
            $trainQuestionInfo = $trainQuestion->getTrainQuestionInfo($params['id'])->toArray();
            $answerList = [];
            if ($trainQuestionInfo['type'] != 4) {
                $answerList = $trainQuestion->getTrainAnswerList($params['id'])->toArray();
            }
        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }
        debug($answerList);
        return $this->view('train-question-edit', [
            'trainQuestionInfo' => $trainQuestionInfo,
            'answerList' => $answerList,
        ]);
    }

    /**
     * 问题详情
     * @author zhugege
     */
    public function trainQuestionDetail()
    {
        $params = $this->getRequestParameters(['id']);
        $trainQuestion = new TrainQuestion();
        $trainQuestionInfo = $trainQuestion->getTrainQuestionInfo($params['id'])->toArray();
        $answerList = [];
        if ($trainQuestionInfo['type'] != 4) {
            $answerList = $trainQuestion->getTrainAnswerList($params['id'])->toArray();
        }

        return $this->view('train-question-detail', [
            'trainQuestionInfo' => $trainQuestionInfo,
            'answerList' => $answerList,
        ]);
    }
}